//
//  WBVisitorView.swift
//  czWB
//
//  Created by Qing on 2018/6/14.
//  Copyright © 2018年 成哥哥. All rights reserved.
//

import UIKit

class WBVisitorView: UIView {
    
    //    登录按钮
     lazy var loginButton: UIButton = UIButton.cz_textButton(title: "登录", fontSize: 18, normalColor: UIColor.darkGray, highlightColor: UIColor.orange)
    //    注册按钮
     lazy var registerButton: UIButton = UIButton.cz_textButton(title: "注册", fontSize: 18, normalColor: UIColor.orange, highlightColor: UIColor.darkGray)
    
    /// 设置访客视图字典信息
    var visitorInfo: [String: String]? {
        
        didSet {
            
            
            guard let imageName = visitorInfo?["imageName"],
                let message = visitorInfo?["message"] else {
                    return
            }
            
            tipLable.text = message
            
//            首页的imgaeName=="", 所以不需要设置
            if imageName == "" {
                return
            }
            
            iconView.image = UIImage(named: imageName)
            houseIconView.isHidden = true
            
        }
        
    }

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
//        self.frame = UIScreen.main.bounds
        
        setupUI()
    
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 私有控件
//    小房子
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitor_house"))
//    小房子背景圈圈
    private lazy var houseIconView: UIImageView = UIImageView(image: UIImage(named: "visitor_house_background"))
//    提示
    private lazy var tipLable: UILabel = UILabel.cz_Lable(withText: "", fontSize: 14)

    
}


// MARK: - 设置界面
extension WBVisitorView {
    
    
    /// 设置UI
    private func setupUI() {
        
        addSubview(iconView)
        addSubview(houseIconView)
        addSubview(tipLable)
        addSubview(loginButton)
        addSubview(registerButton)
        
//        使用代码布局需要把每个要布局的视图的translates属性 设为false
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        
//MARK: - 私有控件布局约束
//        1> 小房子
        addConstraint(NSLayoutConstraint.init(item: iconView,
                                                         attribute: .centerX,
                                                         relatedBy: .equal,
                                                         toItem: self,
                                                         attribute: .centerX,
                                                         multiplier: 1.0,
                                                         constant: 0))
        addConstraint(NSLayoutConstraint.init(item: iconView,
                                                         attribute: .centerY,
                                                         relatedBy: .equal,
                                                         toItem: self,
                                                         attribute: .centerY,
                                                         multiplier: 1.0,
                                                         constant: -60))

//        2>小房子背景圆圈
        addConstraint(NSLayoutConstraint.init(item: houseIconView,
                                              attribute: .centerX,
                                              relatedBy: .equal,
                                              toItem: iconView,
                                              attribute: .centerX,
                                              multiplier: 1.0,
                                              constant: 0))
        addConstraint(NSLayoutConstraint.init(item: houseIconView,
                                              attribute: .centerY,
                                              relatedBy: .equal,
                                              toItem: iconView,
                                              attribute: .centerY,
                                              multiplier: 1.0,
                                              constant: 0))
//        3> tipLable提示
        addConstraint(NSLayoutConstraint.init(item: tipLable,
                                              attribute: .centerX,
                                              relatedBy: .equal,
                                              toItem: houseIconView,
                                              attribute: .centerX,
                                              multiplier: 1.0,
                                              constant: 0))
        addConstraint(NSLayoutConstraint.init(item: tipLable,
                                              attribute: .top,
                                              relatedBy: .equal,
                                              toItem: houseIconView,
                                              attribute: .bottom,
                                              multiplier: 1.0,
                                              constant: 20))
        addConstraint(NSLayoutConstraint.init(item: tipLable,
                                              attribute: .width,
                                              relatedBy: .equal,
                                              toItem: nil,
                                              attribute: .notAnAttribute,
                                              multiplier: 1.0,
                                              constant: 250))
        
//        4>注册按钮
        addConstraint(NSLayoutConstraint.init(item: registerButton,
                                              attribute: .top,
                                              relatedBy: .equal,
                                              toItem: tipLable,
                                              attribute: .bottom,
                                              multiplier: 1.0,
                                              constant: 20))
        addConstraint(NSLayoutConstraint.init(item: registerButton,
                                              attribute: .left,
                                              relatedBy: .equal,
                                              toItem: tipLable,
                                              attribute: .left,
                                              multiplier: 1.0,
                                              constant: 70))
        
//        5>登录按钮
        addConstraint(NSLayoutConstraint.init(item: loginButton,
                                              attribute: .top,
                                              relatedBy: .equal,
                                              toItem: registerButton,
                                              attribute: .top,
                                              multiplier: 1.0,
                                              constant: 0))
        addConstraint(NSLayoutConstraint.init(item: loginButton,
                                              attribute: .left,
                                              relatedBy: .equal,
                                              toItem: registerButton,
                                              attribute: .right,
                                              multiplier: 1.0,
                                              constant: 30))

        //小房子背景圆圈动画
//        1.创建旋转动画
//        2.旋转角度 DOuble.pi>π
//        3.旋转指定角度需要的时间>秒
//        4.旋转重复次数>无限
//        5.动画执行完后不移除
//        6.将动画添加到视图的laye上
        let a = CABasicAnimation(keyPath: "transform.rotation")
        a.toValue = Double.pi//1 * M_PI
        a.duration = 3
        a.repeatCount = MAXFLOAT
        a.isRemovedOnCompletion = false
        houseIconView.layer.add(a, forKey: nil)
    }
    
}
